package com.dromara.coc.util;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * @Author: luoweisong
 * @CreateTime: 2025-10-30  15:07
 * @Description: 模拟鼠标工具类
 */
public class MouseUtil {

    public static void dragMouse(Robot robot, int x, int y, int moveX, int moveY) throws InterruptedException {
        robot.mouseMove(x, y); // 示例坐标 (100, 100)
        Thread.sleep(100);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); // 按下鼠标左键
        Thread.sleep(100);
        robot.mouseMove(moveX, moveY); // 示例坐标 (100, 100)
        Thread.sleep(1000);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); // 释放鼠标左键
    }

    public static void moveMouse(Robot robot, int x, int y) {
        robot.mouseMove(x, y); // 示例坐标 (100, 100)
    }

    public static void clickMouse(Robot robot, int x, int y, long sleep) throws InterruptedException {
        robot.mouseMove(x, y); // 示例坐标 (100, 100)
        Thread.sleep(sleep);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); // 按下鼠标左键
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); // 释放鼠标左键
    }

    public static void scrollMouse(Robot robot, int wheel, long sleep, boolean isInternationalVersion) throws InterruptedException {
        if (isInternationalVersion) {
            robot.keyPress(KeyEvent.VK_CONTROL);
        }
        robot.mouseWheel(wheel);
        Thread.sleep(sleep);
        if (isInternationalVersion) {
            robot.keyRelease(KeyEvent.VK_CONTROL);
        }
    }

    public static boolean isOk() {
        String windowTitle = getActiveWindowTitle();
        return "雷电模拟器".equals(windowTitle);
    }

    public static String getActiveWindowTitle() {
        // 获取前台窗口句柄
        WinDef.HWND foregroundWindow = User32.INSTANCE.GetForegroundWindow();

        if (foregroundWindow == null) {
            return "No active window";
        }

        // 获取窗口标题文本长度
        int titleLength = User32.INSTANCE.GetWindowTextLength(foregroundWindow);

        if (titleLength == 0) {
            return "Untitled window";
        }

        // 创建字符数组来存储窗口标题
        char[] buffer = new char[titleLength + 1];

        // 获取窗口标题
        int result = User32.INSTANCE.GetWindowText(foregroundWindow, buffer, buffer.length);

        if (result == 0) {
            return "Failed to get window title";
        }

        return new String(buffer, 0, result);
    }

    public static void battle(Robot robot, int isMachine, int num, int t, boolean isInternationalVersion) {
        try {

            clickMouse(robot, 1620, 1010, 500);
            if (!isOk()) {
                return;
            }

            clickMouse(robot, 1820, 970, 500);
            if (!isOk()) {
                return;
            }

            clickMouse(robot, 1620, 1010, 10000);
            if (!isOk()) {
                return;
            }

            moveMouse(robot, 1750, 900);
            if (!isOk()) {
                return;
            }

            for (int i = 0; i < 3; i++) {
                scrollMouse(robot, 2, 500, isInternationalVersion);
            }

            for (int i = 0; i < 2; i++) {
                dragMouse(robot, 1745, 935, 1825, 890);
                if (!isOk()) {
                    return;
                }
            }

            clickMouse(robot, 1636, 898, 500);
            if (!isOk()) {
                return;
            }

            // 战争机器技能
            if (isMachine > 0) {
                clickMouse(robot, 1620, 1010, 500);
                if (!isOk()) {
                    return;
                }
            }

            if (num > 0) {
                if (isMachine > 0) {
                    clickMouse(robot, 1650, 1010, 500);
                    if (!isOk()) {
                        return;
                    }
                }
                // 所有兵种
                for (int i = 0; i < (isMachine > 0 ? num : num - 1); i++) {
                    clickMouse(robot, 1636, 898, 200);
                    if (!isOk()) {
                        return;
                    }
                }
            }

            // 结束
            clickMouse(robot, 1620, 980, t);
            if (!isOk()) {
                return;
            }

            clickMouse(robot, 1820, 970, 500);
            if (!isOk()) {
                return;
            }

            clickMouse(robot, 1750, 1010, 500);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void collect(Robot robot, boolean isInternationalVersion) throws InterruptedException {
        Thread.sleep(4000);

        moveMouse(robot, 1750, 1010);
        for (int i = 0; i < 3; i++) {
            scrollMouse(robot, 2, 500, isInternationalVersion);
        }

        dragMouse(robot, 1745, 935, 1645, 1000);
        if (!isOk()) {
            return;
        }

        clickMouse(robot, 1785, 920, 500);
        clickMouse(robot, 1785, 910, 500);
        clickMouse(robot, 1793, 920, 500);
        clickMouse(robot, 1800, 920, 500);
        clickMouse(robot, 1793, 910, 500);
        clickMouse(robot, 1800, 910, 500);
        if (!isOk()) {
            return;
        }

        clickMouse(robot, 1840, 1000, 500);
        if (!isOk()) {
            return;
        }

        clickMouse(robot, 1900, 1000, 500);

    }

    public static void main(String[] args) throws AWTException, InterruptedException {
        /*Thread.sleep(2000);
        getMousePosition();*/
        Robot robot = new Robot();
        int num1 = 2;
        int num2 = 1;
        int num3 = 4;
        int num4 = 1;
        for (int i = 0; i < num1; i++) {
            Thread.sleep(3000);
            System.out.println("第" + (i + 1) + "次执行");
            if (isOk()) {
//                battle(robot, num2, num3, num4);
//                if (i % 5 == 4) {
//                    collect(robot);
//                }
            } else {
                break;
            }
        }
    }

    public static void getMousePosition() {
        Point location = MouseInfo.getPointerInfo().getLocation();
        double x = location.getX();
        double y = location.getY();
        System.out.println("Mouse X: " + x + " Mouse Y: " + y);
    }
}
